package com.heifeng.cloudshelf.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import androidx.appcompat.app.AppCompatDelegate;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.android.tu.loadingdialog.LoadingDialog;
import com.heifeng.cloudshelf.di.component.ActivityComponent;
import com.heifeng.cloudshelf.di.component.DaggerActivityComponent;
import com.heifeng.cloudshelf.di.module.ActivityModule;
import com.heifeng.cloudshelf.utils.NetUtils;

import javax.inject.Inject;

/**
 * MVP activity基类
 */
public abstract class BaseActivity<T extends IBasePresenter> extends SimpleActivity implements IBaseView {

    @Inject
    protected T mPresenter;     //dagger2依赖注入

    public LoadingDialog dialog;
    LoadingDialog.Builder builder;

    private boolean netConnect = true;

    public boolean isNetConnect() {
        return netConnect;
    }

    public void setNetConnect(boolean netConnect) {
        this.netConnect = netConnect;
    }

    /**
     * 显示隐藏进度条
     */
    public void showProgressDialog(String messege) {
        if (TextUtils.isEmpty(messege)) messege = "加载中...";
        builder = new LoadingDialog.Builder(this)
                .setMessage(messege)
                .setCancelable(false);
        if (dialog == null) {
            dialog = builder.create();
            dialog.show();
        } else {
            dialog.show();
        }
    }

    /**
     * 隐藏进度条
     */
    public void dimissProgressDialog() {
        if (dialog != null)
            dialog.dismiss();
    }

    protected ActivityComponent getActivityComponent() {
        return DaggerActivityComponent.builder()
                .appComponent(MyApp.getAppComponent())
                .activityModule(getActivityModule())
                .build();
    }

    protected abstract void initInject();

    protected ActivityModule getActivityModule() {
        return new ActivityModule(this);
    }


    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        ((Activity) this).overridePendingTransition(0, 0);
        initInject();
        if (mPresenter != null) {
            mPresenter.attachView(mContext, this);
        }

        checkNet();
//        NetCheck.getInstance().start();
    }

    protected void checkNet(){
        netConnect = NetUtils.isNetworkConnected(mContext);//获取网络状态
    }


    @Override
    protected void onDestroy() {
        if (mPresenter != null)
            mPresenter.detachView();
        super.onDestroy();
    }


    @Override
    public void useNightMode(boolean isNight) {
        if (isNight) {
            AppCompatDelegate.setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_NO);
        }
        recreate();
    }

    /**
     * @return EditText失去焦点时隐藏键盘
     */
    private static boolean sHideKeyboardTouchOutsideEnabled = true;

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN && sHideKeyboardTouchOutsideEnabled) {
            View v = getCurrentFocus();
            if (v instanceof EditText) {
                Rect outRect = new Rect();
                v.getGlobalVisibleRect(outRect);
                if (!outRect.contains((int) event.getRawX(), (int) event.getRawY())) {
                    v.clearFocus();
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
        }
        return super.dispatchTouchEvent(event);
    }

    public static boolean isHideKeyboardTouchOutsideEnabled() {
        return sHideKeyboardTouchOutsideEnabled;
    }

    public static void setHideKeyboardTouchOutsideEnabled(boolean enable) {
        BaseActivity.sHideKeyboardTouchOutsideEnabled = enable;
    }

    @Override
    public void finish() {
        super.finish();
        ((Activity) this).overridePendingTransition(0, 0);
    }
}